package flower
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	public class Cloth
	{
		
//道具(1), 左右头饰(2),面部装饰(3),头发(4),耳环(5),项链(6),腰带(7),上衣(8),手装饰(9),
//鞋子(10),脚部装饰(11),裙子(12),翅膀(13)
//触角(101),眉(102)，眼睛(103)，嘴(104),脸型(105)
//Typenum 0=left,1=right,2=left+right,3= 裤子或者有袖的上衣(3个位置左右中都有),4(head中的眼睛等 5样)10=body中间(只有一个）5=分成5片的裤子
		private var LoadedClothNum:uint=0//计数 身上的衣服有没有全加载完
		private var arr:Array//穿在身上的衣服数组 
		private var bone:MovieClip//骨骼
		public var BC_List:Object
		public var CanClickBool:Boolean//衣服是否可以点击(脱下）
		public static var CLICK_DOWN_CLOTH:String="click_down_cloth"
		public static var ALL_CLOTH_LOADED:String="all_cloth_loaded"
		private static var url:String=Fairy.IP+"/resource/cloth/swf/"
		//private static var url:String="resource/cloth/swf/"
		public static var huaurl:String=Fairy.IP
		//public static var huaurl:String=""
		private static var cantdownArr:Array=[4,8,12,13,14,101,102,103,104,105]
		public function Cloth(_arr:Array,_bone:MovieClip,_CanClickBool:Boolean=false)
		{
			CanClickBool=_CanClickBool
			var len:uint=_arr.length
			arr=new Array()
			for(var i:uint=0;i<len;i++){
				var id:uint=_arr[i]
				if((id>100000&&id<200000)||(id>600000&&id<700000)){
					arr.push(GoodsInfo.getInfoById(_arr[i]))
				}else{
					
				}
			}
			bone=_bone
			init()
		}
		public function init():void
		{
			var thisObj:Cloth=this
			if(arr.length>0){
				for(var i:uint=0;i<arr.length;i++){
						
						loadOneCloth(arr[i])
				}
			}
		}
		/**
		 * 
		 * @param id
		 * 这个type上有没有穿的东西，有的话返回ID
		 */		
		public function getClothID(type:uint):uint
		{
			for(var i:uint=0;i<arr.length;i++){
				var clothedtype:uint=GoodsInfo.getInfoById(arr[i].ID).ClothType
				if(clothedtype==type){
					return arr[i].ID
				}
			}
			return 0
		}
		/**
		 * id
 4
 Uint
 物品id
 
Op_type
 4
 Uint
 操作类型(0，脱下；1，穿上)
 
id
 4
 Uint
 物品id(0表示没有装扮换下来)
		 * 
		 * */
		public function ChangeCloth(id:uint,type:uint,downid:uint):void
		{
			//换时会有２个。大人（书中）换。小人（场景中）换
			if(type==1){
				//(0，脱下；1，穿上)
				if(downid!=0){
					//有装扮换下来
					downCloth(downid)
					
				}
				var obj:Object=GoodsInfo.getInfoById(id)
				arr.push(obj)
				loadOneCloth(obj)
			}else{
				downCloth(id)
			}
		}
		/**
		 * 脱下装扮
		 * */
		public function downCloth(id:uint):void
		{
			
			//if(cantdownArr.indexOf(arr[i].ClothType)>=0)return
			for(var i:uint=0;i<arr.length;i++){
				if(arr[i].ID==id){
					var delobj:Object=arr.splice(i,1)[0]
					unloadOneCloth(delobj)
					return
				}
			}
		}
		/**
		 * 脱下一件衣服
		 **/
		public function unloadOneCloth(obj:Object):void
		{
			if(obj.Typenum==2){
					unloadcloth(clothmc(obj.ClothType,0))
					unloadcloth(clothmc(obj.ClothType,1))
				}else if(obj.Typenum==10){
					unloadcloth(clothmc(obj.ClothType,2))
				}else if(obj.Typenum==1){
					unloadcloth(clothmc(obj.ClothType,1))
				}else if(obj.Typenum==0){
					unloadcloth(clothmc(obj.ClothType,0))
				}else if(obj.Typenum==4){
					unloadcloth(clothmc(obj.ClothType,2))
				}else if(obj.Typenum==3){
					unloadcloth(clothmc(obj.ClothType,0))
					unloadcloth(clothmc(obj.ClothType,1))
					unloadcloth(clothmc(obj.ClothType,2))
				}
		}
		/**
		 * //种子，水壶，药，道具等 
		 * 
		 * **/
		public function loadOneTool(obj:Object):void
		{
			if(obj.Type==2){
				//seed
				loadcloth(huaurl+"resource/home/seed/inhand/"+obj.ID+".swf",clothmc(1,0),obj)
			}else if(obj.Type==3){
				//tool
				if(Fairy.Info.User_Sex){
					loadcloth(huaurl+"resource/home/tool/maninhand/"+obj.ID+".swf",clothmc(1,0),obj)
				}else{
					loadcloth(huaurl+"resource/home/tool/inhand/"+obj.ID+".swf",clothmc(1,0),obj)
				}
			}else if(obj.Type==13){
				//dust
				loadcloth(huaurl+"resource/home/dust/inhand/dust.swf",clothmc(1,0),obj)
			}
				
		}
		/**
		 * //种子，水壶，药，道具等 
		 * 
		 * **/
		public function unloadOneTool():void
		{
			unloadcloth(bone.lefthand.Tool)
		}
		/**
		 * 加载一样衣服或者装饰
		 * 
		 * **/
		public function loadOneCloth(obj:Object):void
		{
			var mc0:MovieClip=clothmc(obj.ClothType,0)
			var mc1:MovieClip=clothmc(obj.ClothType,1)
			var mc2:MovieClip=clothmc(obj.ClothType,2)
			var mc3:MovieClip=clothmc(obj.ClothType,3)
			var mc4:MovieClip=clothmc(obj.ClothType,4)
				if(obj.Typenum==2){
					unloadcloth(mc0)
					unloadcloth(mc1)
					loadcloth(url+"left/"+obj.ID+".swf",mc0,obj)
					loadcloth(url+"right/"+obj.ID+".swf",mc1,obj)
				}else if(obj.Typenum==10){
					unloadcloth(mc0)
					unloadcloth(mc1)
					unloadcloth(mc2)
					loadcloth(url+"body/"+obj.ID+".swf",mc2,obj)
				}else if(obj.Typenum==1){
					unloadcloth(mc0)
					unloadcloth(mc1)
					loadcloth(url+"right/"+obj.ID+".swf",mc1,obj)
				}else if(obj.Typenum==0){
					unloadcloth(mc0)
					unloadcloth(mc1)
					loadcloth(url+"left/"+obj.ID+".swf",mc0,obj)
				}else if(obj.Typenum==4){
					loadcloth(url+"face/"+obj.ID+".swf",mc2,obj)
				}else if(obj.Typenum==3){
					unloadcloth(mc0)
					unloadcloth(mc1)
					unloadcloth(mc2)
					unloadcloth(mc3)
					unloadcloth(mc4)
					loadcloth(url+"left/"+obj.ID+".swf",mc0,obj)
					loadcloth(url+"right/"+obj.ID+".swf",mc1,obj)
					loadcloth(url+"body/"+obj.ID+".swf",mc2,obj)
				}else if(obj.Typenum==5){
					unloadcloth(mc0)
					unloadcloth(mc1)
					unloadcloth(mc2)
					unloadcloth(mc3)
					unloadcloth(mc4)
					
					loadcloth(url+"left/"+obj.ID+".swf",mc0,obj)
					loadcloth(url+"right/"+obj.ID+".swf",mc1,obj)
					loadcloth(url+"body/"+obj.ID+".swf",mc2,obj)
					loadcloth(url+"leftTrouser/"+obj.ID+".swf",mc3,obj)
					loadcloth(url+"rightTrouser/"+obj.ID+".swf",mc4,obj)
				}
		}
		/***
		 * 取得衣服要加载到哪个MC
		 * 
		 * 
		 * */
		public function clothmc(ClothType:uint,leftrightbody:uint):MovieClip
		{
			//Typenum=0=left,1=right,2=body中间(一个） 

//道具(1), 左右头饰(2),面部装饰(3),头发(4),耳环(5),项链(6),腰带(7) ,上衣 (8),手装饰(9), 
//鞋子(10),脚部装饰(11),裙子(12),翅膀(13), 裤子(14),手臂装饰  （15） 

//触角（101）,眉(102)，眼睛(103)，嘴(104),脸型(105) 
			var mc:MovieClip=new MovieClip()
			switch(ClothType){
				case 1:
				if(leftrightbody==0){
					mc=bone.lefthand.Tool
				}else{
					mc=bone.righthand.Tool
				}
				break;
				case 2:
				if(leftrightbody==0){
					mc=bone.head.Leftdress
				}else{
					mc=bone.head.Rightdress
				}
				break;
				case 3:
				mc=bone.head.Mask		
				break;
				case 4:
				if(leftrightbody==0){
					mc=bone.head.Fronthair
				}else{
					mc=bone.backhair
				}
				break;
				case 5:
				mc=bone.head.Earring		
				break;
				case 6:
				mc=bone.body.Necklace		
				break;
				case 7:
				mc=bone.belly.Belt		
				break;
				case 8:	
				if(leftrightbody==0){
					mc=bone.leftarm.Dress
				}else if(leftrightbody==1){
					mc=bone.rightarm.Dress
				}else{
					mc=bone.body.Cloth	
				}
				break;
				case 9:
				if(leftrightbody==0){
					mc=bone.lefthand.Dress
				}else{
					mc=bone.righthand.Dress
				}
				break;
				case 10:
				if(leftrightbody==0){
					mc=bone.leftfoot.Shoe
				}else{
					mc=bone.rightfoot.Shoe 
				}
				break;
				case 11:
				if(leftrightbody==0){
					mc=bone.leftfoot.Anklet
				}else{
					mc=bone.rightfoot.Anklet
				}
				break;
				case 12:
				if(leftrightbody==0){
					mc=bone.leftleg.Dress
				}else if(leftrightbody==1){
					mc=bone.rightleg.Dress
				}else if(leftrightbody==2){
					mc=bone.belly.Skirt
				}else if(leftrightbody==3){
						mc=bone.leftfoot.Trouser
					if(!mc){
						mc=bone.belly.Skirt
					}
				}else if(leftrightbody==4){
					
					mc=bone.rightfoot.Trouser
					if(!mc){
						mc=bone.belly.Skirt
					}
				}
					
				break;
				case 13:
				mc=bone.wing
				break;
				case 15:
				if(leftrightbody==0){
					mc=bone.leftarm.Dress
				}else{
					mc=bone.rightarm.Dress
				}
				break;
				
 
				case 101:
				mc=bone.head.Feeler
				break;
				case 102:
				mc=bone.head.Eyebrow		
				break;
				case 103:
				mc=bone.head.Eye		
				break;
				
				case 104:
				mc=bone.head.Mouth		
				break;
				
				case 105:
				mc=bone.head.Face		
				break;
			}
			//bone.head.cacheAsBitmap=true
			mc.cacheAsBitmap=true
			return mc
		}
		/**
		 * unLOAD CLOTH
		 * 
		 * */
		public function unloadcloth(mc:MovieClip):void
		{
			while(mc.numChildren>0){
				mc.removeChildAt(0)
			}
		}
		/**
		 * LOAD CLOTH
		 * 
		 * */
		public function backCloth(e:MouseEvent):void
		{
			if(!CanClickBool)return
			var mc:MovieClip=e.currentTarget as MovieClip
			var ClothType:uint=GoodsInfo.getInfoById(mc.ID).ClothType
			if(cantdownArr.indexOf(ClothType)>=0)return
			if(mc.numChildren>0){
				//downCloth(mc.ID)
				//PeopleLogic.getWoner().dispatchEvent(new EventTaomee(CLICK_DOWN_CLOTH,{ID:mc.ID}))	
				
				//SocketConnection.send(CommandID.CLOTH_CHANG,mc.ID,)
			}
			
			
		}
		/**
		 * LOAD CLOTH
		 * 
		 * */
		public function loadcloth(url:String,mc:MovieClip,obj:Object):void
		{
			mc.ID=obj.ID
			//BC.addEvent(this,mc,MouseEvent.CLICK,backCloth)
			while(mc.numChildren>0){
				mc.removeChildAt(0)
			}
			var tempLoader:Loader=new Loader()
			mc.addChild(tempLoader)
			tempLoader.load(new URLRequest(url))
			//try{LoaderList.getInstance().addItem(tempLoader,VL.getURLRequest(url),LoaderList.STANDARD,true);}catch(E:*){}
			//BC.addEvent(this,tempLoader.contentLoaderInfo,IOErrorEvent.IO_ERROR,clothloadIOError)
			//BC.addEvent(this,tempLoader.contentLoaderInfo,Event.INIT,clothloaded)
		}
		
		
		public function clothloaded(E:Event):void
		{
			//isAllClothLoaded()
			var mcroot:*=E.currentTarget.content.getChildAt(0)
			if(mcroot is MovieClip){
				mcroot.stop()
			}
			//BC.removeEvent(this,E.currentTarget)
			
		}
		public function get wing():MovieClip
		{
			return bone.wing
		}
		public function destroy():void
		{
			//BC.removeEvent(this)
			//GC.clearAll(bone)
			bone=null
			arr=null
		}

	}
}